package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author faiz
 * @since 2021-04-21
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVo> getChapterVideoById(String courseId) {
        //1 根据课程id查询课程里面的所有章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        List<EduChapter> eduChapterList = list(chapterQueryWrapper);
        //2 根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(videoQueryWrapper);
        //创建list集合，用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();
        //3 遍历查询章节list集合封装
        for (EduChapter eduChapter : eduChapterList) {
            //eduChapter中的值放到，ChapterVo里面去
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            //4 遍历查询小节list集合，进行封装
            //创建list集合封装小节
            List<VideoVo> videoVoList = new ArrayList<>();
            for (EduVideo eduVideo : eduVideoList) {
                //判断小节里面是否属于该章节
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    //封装小节
                    videoVoList.add(videoVo);
                }
            }
            //把封装后的小节部分再set进去
            chapterVo.setChildren(videoVoList);
            //把ChapterVo放到最终的list集合中
            finalList.add(chapterVo);
        }

        return finalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        //先查询小节表
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        //查询改小节中的记录数
        int count = eduVideoService.count(wrapper);
        if (count > 0) {
            //能查询小节
            throw new GuliException(20001, "不能删除");
        } else {
            boolean result = removeById(chapterId);
            return result;
        }
    }

    @Override
    public List<ChapterVo> getChapterVideoById2(String courseId) {
        //1 根据课程id查询课程里面的所有章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        List<EduChapter> eduChapterList = list(chapterQueryWrapper);
        //2 根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(videoQueryWrapper);
        //创建list集合，用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        //先将每个小节的视频分类
        Map<String, List<EduVideo>> videoList = eduVideoList.stream().collect(Collectors.groupingBy(v -> v.getChapterId(), Collectors.toList()));
        for (EduChapter eduChapter : eduChapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            List<EduVideo> chapterVideoList = videoList.get(eduChapter.getId());
            for (EduVideo eduVideo : chapterVideoList) {
                VideoVo videoVo = new VideoVo();
                BeanUtils.copyProperties(eduVideo, videoVo);
                chapterVo.getChildren().add(videoVo);
            }
            finalList.add(chapterVo);
        }
        return finalList;
    }

    //根据id删除课程章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        remove(wrapper);
    }
}
